package cn.bone.demo.quartz;

import cn.bone.demo.log.Hello;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * Created by MD-01 on 2016/10/10.
 */
public class QuartzTest {

    public static void main(String[] args){

        SchedulerFactory schedulerFactory = new StdSchedulerFactory();
        Scheduler scheduler = null;
        try {
            scheduler = schedulerFactory.getScheduler();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }
        JobDetail job = JobBuilder.newJob(HelloJob.class)
                .withIdentity("job1","group1")
                .build();
        /*Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("trigger1","group1")
                .startNow()
                .withSchedule(
                        SimpleScheduleBuilder.simpleSchedule()
                                .withIntervalInSeconds(10)
                                .repeatForever())
                .build();*/

        LocalDateTime localDateTime = LocalDateTime.of(2016,10,9,16,44);
        ZoneId zone = ZoneId.systemDefault();
        Instant instant = localDateTime.atZone(zone).toInstant();
        Date date = Date.from(instant);

        Trigger trigger = TriggerBuilder.newTrigger()
                .withIdentity("job1","group1")
                .startAt(date)
                .withSchedule(
                        CalendarIntervalScheduleBuilder.calendarIntervalSchedule()
                                .withInterval(3, DateBuilder.IntervalUnit.DAY)
                )
                .build();


        try {
            scheduler.scheduleJob(job, trigger);
            scheduler.start();
        } catch (SchedulerException e) {
            e.printStackTrace();
        }


    }
}
